import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
  HttpException,
} from '@nestjs/common';
import { Response } from 'express';

@Catch()
export class HttpErrorFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const oldRes: any = exception.getResponse();
    response.status(oldRes.statusCode).json({
      code: oldRes.statusCode,
      msg: oldRes.message,
      timestamp: new Date().toISOString(),
    });
  }
}
